    var ads = [];
<?php foreach ($datas as $data): ?>
        ads.push({"id":'<?php echo $data['id'] ?>',"title":'<?php echo $data['title'] ?>',"position":'<?php echo $data['position'] ?>',"type":'<?php echo $data['type'] ?>'})
<?php endforeach; ?>
    $().ready(function(){
        $('#ad_position').change(function(){
            var html ='';
            var position =$('#ad_position option:selected').val();
            for(var y=0;y<ads.length;y++){
                if(ads[y].position == position){
                    html +="<option value='"+ads[y].id+"'>"+ads[y].title+"</option>";

                }
            }
            $('#custom_ad').html(html);
        });
        if($('#ad_position').val()) {$('#ad_position').trigger('change');}
    });